local piaojin = fk.CreateSkill{
  name = "hanqing__piaojin",
  tags = { Skill.Compulsory },
}

Fk:loadTranslationTable{
  ["hanqing__piaojin"] = "殍殣",
  [":hanqing__piaojin"] = "锁定技，游戏开始时，你获得一张【兵粮寸断】，此牌生效或被你弃置后，你获得之；此牌被其他角色弃置后，将之置入你的判定区。",

  ["@@hanqing__piaojin"] = "殍殣",

  ["$hanqing__piaojin1"] = "既不为刀下厉鬼，何不做殍殣土灰！",
  ["$hanqing__piaojin2"] = "围于其府，刀兵不举，尚能饮稀，有何不悦？",
  ["$hanqing__piaojin3"] = "主父尚掌大权，今陡然逸出，我等恐难再还！",
}

piaojin:addEffect(fk.GameStart, {
  mute = true,
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(piaojin.name)
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(piaojin.name, math.random(2))
    room:notifySkillInvoked(player, piaojin.name, "drawcard")
    local id = room:printCard("supply_shortage", Card.Spade, 10).id
    room:setPlayerMark(player, piaojin.name, id)
    local card = Fk:getCardById(id)
    room:moveCardTo(card, Card.PlayerHand, player, fk.ReasonJustMove, piaojin.name, nil, true, player, "@@hanqing__piaojin")
  end,
})

piaojin:addEffect(fk.CardEffectFinished, {
  mute = true,
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(piaojin.name) and
      data.card.id == player:getMark(piaojin.name) and table.contains(player.room.discard_pile, data.card.id)
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(piaojin.name, math.random(2))
    room:notifySkillInvoked(player, piaojin.name, "drawcard")
    room:moveCardTo(data.card, Card.PlayerHand, player, fk.ReasonJustMove, piaojin.name, nil, true, player)
  end,
})

piaojin:addEffect(fk.CardEffectCancelledOut, {
  mute = true,
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(piaojin.name) and
      data.card.id == player:getMark(piaojin.name) and player.room:getCardArea(data.card) == Card.Processing
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(piaojin.name, math.random(2))
    room:notifySkillInvoked(player, piaojin.name, "drawcard")
    room:moveCardTo(data.card, Card.PlayerHand, player, fk.ReasonJustMove, piaojin.name, nil, true, player)
  end,
})

piaojin:addEffect(fk.AfterCardsMove, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(piaojin.name) then
      local id = player:getMark(piaojin.name)
      if not table.contains(player.room.discard_pile, id) then return end
      for _, move in ipairs(data) do
        if move.toArea == Card.DiscardPile and move.moveReason == fk.ReasonDiscard and move.proposer then
          for _, info in ipairs(move.moveInfo) do
            if info.cardId == id then
              if move.proposer == player then
                return true
              elseif not player:hasDelayedTrick("supply_shortage") and not table.contains(player.sealedSlots, Player.JudgeSlot) then
                return true
              end
            end
          end
        end
      end
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local id = player:getMark(piaojin.name)
    local yes = 0
    for _, move in ipairs(data) do
      if move.toArea == Card.DiscardPile and move.moveReason == fk.ReasonDiscard and move.proposer then
        for _, info in ipairs(move.moveInfo) do
          if move.proposer == player and info.cardId == id then
            yes = 1
            break
          end
        end
      end
    end
    if yes == 1 then
      player:broadcastSkillInvoke(piaojin.name, math.random(2))
      room:notifySkillInvoked(player, piaojin.name, "drawcard")
      room:moveCardTo(id, Card.PlayerHand, player, fk.ReasonJustMove, piaojin.name, nil, true, player)
    else
      player:broadcastSkillInvoke(piaojin.name, 3)
      room:notifySkillInvoked(player, piaojin.name, "negative")
      room:moveCardTo(id, Card.PlayerJudge, player, fk.ReasonJustMove, piaojin.name, nil, true, player)
    end
  end,
})

return piaojin
